package com.example.qundui.learndemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.example.qundui.learndemo.netutils.INet;
import com.example.qundui.learndemo.netutils.Netutils;
import com.example.qundui.learndemo.netutils.SSLContextUtil;
import com.example.qundui.learndemo.netutils.StringTools;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.BitmapCallback;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import okhttp3.Call;

/**
 * Created by qundui on 2017/4/24.
 */

public class HttpsDemo extends Activity {
    private Handler handle=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//           String obj = (String) msg.obj;
//            Log.i("gsc","==="+obj);

        }
    };
    final String url="https://bmc.huochexia.com/qundui-manager/userfiles/9a29fe156da64c66adf862f912f0eeaf/images/banner/2016/12/QQ图片20161219175344.png";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpsdemo);
        iv= (ImageView) findViewById(R.id.iv);
    }
    ImageView iv;
    public void click(View view){
        Netutils netutils=new Netutils();
//           final String url="https://bmc.huochexia.com/qundui-manager/userfiles/9a29fe156da64c66adf862f912f0eeaf/images/banner/2016/12/QQ图片20161219175344.png";
//final String url="https://kyfw.12306.cn/otn/";
//      //final String url="https://www.baidu.com/";
//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                getData(url);
//            }
//        }.start();



//        netutils.DoGet(null, url, new INet() {
//            @Override
//            public void onStart(int what) {
//
//            }
//
//            @Override
//            public void onSucceed(int what, Response<String> response) {
//                Log.i("gsc","chenggong="+response.get());
//            }
//
//            @Override
//            public void onFailed(int what, Response<String> response) {
//             Log.i("gsc","fail="+response.getException().getMessage());
//            }
//
//            @Override
//            public void onFinish(int what) {
//
//            }
//        });
//getNet();
        //getHy();
//        getNohhtp();
        getOkgo();
    }

    private void getOkgo() {
//        OkGo.get(url).execute(new com.lzy.okgo.callback.StringCallback() {
//            @Override
//            public void onSuccess(String s, Call call, okhttp3.Response response) {
//               Log.i("gsc","-----okgo-----"+s);
//            }
//        });
        OkGo.get(url).execute(new BitmapCallback() {
            @Override
            public void onSuccess(Bitmap bitmap, Call call, okhttp3.Response response) {
                iv.setImageBitmap(bitmap);
            }
        });
    }
    private void getNohhtp() {
        Request<String> request = NoHttp.createStringRequest("https://kyfw.12306.cn/otn/", RequestMethod.GET);
        request.setSSLSocketFactory(SSLContextUtil.getDefaultSLLContext().getSocketFactory());
        RequestQueue requestQueue = NoHttp.newRequestQueue();
        requestQueue.add(1, request, new OnResponseListener<String>() {
            @Override
            public void onStart(int what) {

            }

            @Override
            public void onSucceed(int what, Response<String> response) {
            Log.i("gsc","----"+response.get());
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }

            @Override
            public void onFinish(int what) {

            }
        });
    }

    private void getHy() {
        OkHttpUtils.get().url("https://kyfw.12306.cn/otn/").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                Log.i("gsc","ok=="+response);

            }
        });
    }
    private void getNet() {
        RequestParams param=new RequestParams("https://kyfw.12306.cn/otn/");
        param.setSslSocketFactory(SSLContextUtil.getSSLContext().getSocketFactory());
        x.http().request(HttpMethod.GET, param, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
               Log.i("gsc",result);
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("gsc",ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Log.i("gsc",cex.getMessage());
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getData(String url1)  {

        try {
            URL url = new URL(url1);
            HttpURLConnection  urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
//             设置SSLSocketFoactory，这里有两种：1.需要安全证书 2.不需要安全证书(绕过证书)
            if (urlConnection instanceof HttpsURLConnection) { // 是Https请求
                SSLContext sslContext = SSLContextUtil.getSSLContext();
                if (sslContext != null) {
                    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
                    ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslSocketFactory);
                }
            }
            urlConnection.setConnectTimeout(8 * 1000);
            urlConnection.setReadTimeout(8 * 1000);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200) { // 请求成功
                InputStream inputStream = urlConnection.getInputStream();
             Log.i("gsc","----"+StringTools.StringtoInpustream(inputStream));
                inputStream.close();
            }
            urlConnection.disconnect();

        }catch (Exception e){

        }



    }
}
